<!DOCTYPE html>
<html>
	<!--巡检项目详情的新模块代码-->
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title>现场检查详情</title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/aui.css" />
		<link rel="stylesheet" href="../../script/ccssoft/css/ccssoft-lite.css" />
		<link rel="stylesheet" type="text/css" href="../../css/aui-flex.css" />
	</head>
	<body>
		<!--下拉列表-->
		<script id="dict-template" type="text/x-dot-template">
			{{ for(var i=0, len=it.length; i < len; i++) {}}
			<option value="{{=it[i].hiddencontent}}">{{=it[i].hiddencontent}}</option>
			{{ } }}
		</script>
		<script id="m-template" type="text/x-dot-template">
			{{ for(var i=0, len=it.length; i < len; i++) {}}
			<option value="{{=it[i].itemvalue}}">{{=it[i].itemname}}</option>
			{{ } }}
		</script>
		<div class="aui-content">
			<form>
				<ul id="list-view" class="aui-list-view"></ul>
			</form>
		</div>
		<div style="height:50px;"></div>
		<div id="downloadBtn" class="fixed_bottom" style="display: none;">
			<button class="outline center aui-btn aui-btn-success" onclick="downloadPic();">
				下载图片
			</button>
		</div>
		<!--项目详情-->
		<script id="parol-template" type="text/html">
			<input type="hidden" name="detailid" value="{{=it.detailid}}" />
			<input type="hidden" name="itemid" value="{{=it.itemid}}" />
			<input type="hidden" name="weight" value="{{=it.weigth}}" />
			<input type="hidden" name="isFinish" value="Y" />
			<input type="hidden" name="parent_name" value="{{=it.parent_name}}" />
			{{? it.check_object == '3' }}
			<li class="aui-list-view-cell ccssoft-aui-list-view-divider">
			站址信息
			</li>
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			站址编码
			</div>
			<div  id="stationName" class="item-right">{{=it.st_deviceid || ''}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			站址名
			</div>
			<div  id="stationName" class="item-right">{{=it.stationName}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			运维ID
			</div>
			<div id="stationCode" class="item-right">{{=it.workid}}</div>
			</li>
				<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			站址来源
			</div>
			<div  class="item-right">
			{{? it.st_sitesource == '1' }}
			新建站
			{{?? it.st_sitesource == '2' }}
			存量-移动
			{{?? it.st_sitesource == '3' }}
			存量-联通
			{{?? it.st_sitesource == '4' }}
			存量-电信
			{{?? it.st_sitesource == '6' }}
			未填写
			{{?}}</div>
			</li>
			{{??}}
			<li class="aui-list-view-cell ccssoft-aui-list-view-divider">
			代维驻点信息
			</li>
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			代维组织机构名称
			</div>
			<div  id="base_name" class="item-right">{{=it.base_name}}</div>
			</li>
			{{?}}
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			所属省份
			</div>
			<div id="province" class="item-right">{{=it.provincename}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			所属地市
			</div>
			<div id="city" class="item-right">{{=it.cityname}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			所属区县
			</div>
			<div id="country" class="item-right">{{=it.countyname}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list_arrow">
			<div class="item-left">
			代维公司
			</div>
			<div  id="repair_company" class="item-right">{{=it.repair_company || ''}}</div>
			</li>
			{{? it.check_object == '3' }}
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			巡查专业
			</div>
			<div id="country" class="item-right">{{=it.major}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			巡查内容
			</div>
			<div id="country" class="item-right">{{=it.content}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			巡查方法
			</div>
			<div id="country" class="item-right">{{=it.ideal}}</div>
			</li>
			{{??}}
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			负责人
			</div>
			<div id="country" class="item-right">{{=it.username}}</div>
			</li>
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			负责人电话
			</div>
			<div id="country" class="item-right">{{=it.userphone!=null?it.userphone:''}}</div>
			</li>
			{{?}}
			<li class="aui-list-view-cell ccssoft-aui-list-view-divider">
			巡查情况填写
			</li>
			<li class="aui-list-view-cell" >
			<input class="aui-radio" type="radio" name="resulttype"  value="Y" {{=(it.iscontent&&it.iscontent == 'Y'||it.resultType&&it.resultType == 'Y')?'checked="checked"':''}}  {{=(!it.editEnabled)?'disabled=true':''}}><div class="aui-radio-name">满足</div>
			<input class="aui-radio" type="radio" name="resulttype"  value="N" {{=(it.iscontent&&it.iscontent == 'N'||it.resultType&&it.resultType == 'N')?'checked="checked"':''}} {{=(!it.editEnabled)?'disabled=true':''}} style="margin-left: 10px;"><div class="aui-radio-name">不满足</div>
			{{? it.check_object == '3' }}
			<input class="aui-radio" type="radio" name="resulttype"  value="V" {{=(it.iscontent&&it.iscontent == 'V'||it.resultType&&it.resultType == 'V')?'checked="checked"':''}} {{=(!it.editEnabled)?'disabled=true':''}} style="margin-left: 10px;"><div class="aui-radio-name">不涉及</div>
			{{?}}
			</li>
			<li class="aui-list-view-cell" data-win="list-image">
			<div class="item-left">
			巡查备注
			</div>
			<div class="item-right">
			<textarea id="remark" name="remark" class="required" data-valid="isStander" data-error="不满足要求，<巡查备注>不能为空！" {{=(!it.editEnabled)?'disabled=true':''}}  style="width:100%;height:100px; margin-bottom:0px;">{{=(it.remark!=null && it.remark!= 'null')?it.remark:''}}</textarea>
			</div>
			</li>
			<li class="aui-list-view-cell ccssoft-aui-list-view-divider">
			图片列表
			</li>
			<li class="aui-list-view-cell" data-win="list-image"  >
			<img id="add" src="../../image/icons/png/icon_addpic_focused.png" width="50" height="50" style="float: left;" onclick="addPic()"/>
			</li>
		</script>
		<script id="img-template" type="text/html">
			<li imgpath="{{=it.imgpath}}" class="aui-list-view-cell aui-img aui-flex-col">
			<div class=" aui-flex-item-2">
			<img class="aui-img-object aui-pull-left" src="{{=it.imgpath}}" onclick="imageUtil.show('{{=it.imgpath}}');">
			</div>
			<div class="aui-pull-left aui-flex-item-8">
			{{=(it.phototime!=null && it.phototime!= 'null')?it.phototime:''}}
			<p class='aui-ellipsis-1'>
			经度:{{=it.lon}}
			</p>
			<p class='aui-ellipsis-1'>
			纬度: {{=it.lat}}
			</p>
			<p >
			地址:{{=it.photoaddress}}
			</p>
			</div>
			{{?typeof(it.isDel) == 'undefined' || it.isDel}}
			<div class=" aui-flex-item-2 aui-flex-row aui-flex-middle aui-flex-center" >
			<button class="aui-btn aui-btn-danger" onclick="removeImg('{{=it.imgpath}}');">X</button>
			</div>
			{{?}}
			</li>
		</script>
	</body>
	<!-- third -->
	<script src="../../script/jquery-1.11.3.min.js"></script>
	<script src="../../script/template-native.js"></script>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/aui.js"></script>
	<script type="text/javascript" src="../../script/doT.min.js"></script>
	<script src="../../script/GPS.js"></script>
	<script src="../../script/debug.js"></script>
	<script src='../../script/constant.js'></script>
	<script type="text/javascript" src="../../script/config.js"></script>
	<script type="text/javascript" src="../../script/common.js"></script>
	<script type="text/javascript" src="../../script/navigate.js"></script>
	<script type="text/javascript" src="../../script/client.api.js"></script>
	<script type="text/javascript" src="../../script/xunjian_db.js"></script>
	<script type="text/javascript" src="../../script/utils/ImageUtil.js"></script>
	<script type="text/javascript" src="../../script/jquery-validate.js"></script>
	<script type="text/javascript" src="../../script/utils/FormUtil.js"></script>
	<script>
		var taskid, projectid, projectname, isFinish, isMonitor, objtype, devname, taskstate;
		var imageUtil;
		var user;
		var modelType = $api.getStorage("modelType");
		var lastLocation;
		var submission = 'ONLY_SUBMIT_TEXT';
		var hiddenList;
		apiready = function() {
			user = $api.getStorage('user');
			lastLocation = $api.getStorage('LAST_GPS_LOCATION');
//			if("Y"!=api.pageParam.isrollback){
//			$api.setStorage("rollback","N");
//			}
//			console.log("buchongzhi:"+$api.getStorage("rollback")+"~~~~"+api.pageParam.isrollback);
			initHeader();
			if (!debug) {
				db_init();
			}
			imageUtil.init({
				uploadFn : $client.uploadCheckImage,
				saveFn : db_saveCheckPic
			});
			formUtil.init({
				submitFn : $client.saveCheckProjectResult,
				btnObj : $('#submitBtn'),
				callback : function(ret, err) {
						api.hideProgress();
					if (ret && ret.success) {
						if (imageUtil.getImageArray().length) {
							if (submission == 'ONLY_SUBMIT_TEXT') {
								saveLocal();
							} else {
								imageUtil.upload(function(count) {
									api.execScript({
										name : 'check_projectList',
										frameName : 'checkProjectListFrm',
										script : 'refreshList();'
									});
									setTimeout(function() {
										api.closeWin();
									}, 1000);
								});
							}
						} else {
							api.toast({
								msg : '提交成功!'
							});
							api.execScript({
								name : 'check_projectList',
								frameName : 'checkProjectListFrm',
								script : 'refreshList();'
							});
							setTimeout(function() {

								api.closeWin();
							}, 1100);
						}
					} else {
						showToast("上传失败!");
					}
				},
				rules : {
					isStander : function(value, errorMsg, el) {
//						console.log("是否满足："+$("input[type='radio']:checked").val());
						var isStander = $('input[name=isStander]').is(':checked');
						if($("input[type='radio']:checked").val()==undefined){
						if (!formUtil.getHasError()) {
									formUtil.setHasError(true);
									return "巡查结果必选";
								}
						}

						if (($("input[type='radio']:checked").val()=='N')) {
							if (value == '') {
								if (!formUtil.getHasError()) {
									formUtil.setHasError(true);
									return errorMsg;
								}
							}
							if (!imageUtil.getImageArray().length) {
								if (!formUtil.getHasError()) {
									formUtil.setHasError(true);
									return "不满足要求，<上传图片>不能为空！";

								}
							}
						}
					}

				}
			});
			projectid = api.pageParam.detailid;
			projectname = api.pageParam.itemname || '';

			loadProjectDetail();


		};

		function saveLocal() {
					imageUtil.save(function(num) {
									var length = imageUtil.getImageArray().length;
									if (num) {
										//刷新首页的图片上传模块头标
										api.execScript({
											name : 'home_win',
											frameName : 'footer_tab_1',
											script : 'setofflineUploadCount();'
										});
										setTimeout(function() {
											//刷新单个列表项
											api.execScript({
												name : 'check_projectList',
												frameName : 'checkProjectListFrm',
												script : 'refreshList();'
											});
											api.hideProgress();
											api.closeWin();
										}, 2000);
									}
								});

		}
		/**
		 *加载巡检项目详情
		 */
		function loadProjectDetail() {

			var desc = api.pageParam.desc;
			var resultType = api.pageParam.resultType;
			console.log("详情：" + resultType);
			var major = desc.substring(desc.indexOf("：") + 1, desc.indexOf("：", desc.indexOf("：") + 1));
			var content = desc.substring(desc.lastIndexOf("：") + 1);
			var ideal = desc.substring(desc.indexOf("：", desc.indexOf("：") + 1) + 1, desc.lastIndexOf("："));
			api.pageParam.major = major;
			api.pageParam.content = content;
			api.pageParam.ideal = ideal;
			api.pageParam.username = user.username;
			api.pageParam.userphone = user.mobilephone;
			renderTemp('list-view', 'parol-template', api.pageParam, false);
			if(!api.pageParam.editEnabled){
			$('#add').parent().hide();
			api.toast({
	            msg:'该任务还没激活,不能进行任务操作'
            });
			}

			if(api.pageParam.iscontent==null){
			$("input[type='radio']:checked").each(function(index) {
					$('#add').parent().hide();
				$(this).attr("disabled", 'true').siblings().click(function() {
					return false;
				});
			showMask("加载中...");
			$client.getCheckImgList({
				detailid : api.pageParam.detailid
			}, function(ret, err) {
				hideMask();
				if (ret) {
					if (ret.success) {
						var imgList = ret.imgList;
						var historyUrl = imageUtil.getDownloadList();
						if (imgList && imgList.length > 0) {
							for (var i = 0, size = imgList.length; i < size; i++) {
								var imageUrl = imgList[i];
								historyUrl.push(imageUrl);
							}
							$('#downloadBtn').show();

						}
					} else {
						showToast(ret.data_info);
					}
				} else {
					showToast(err.msg);
				}
			});


			});
			}

			//
		}

		function displaynum(x) {
			var num = new Number(x);
			return num.toFixed(6);
		}

		/**
		 * 添加图片
		 */
		function addPic() {
			if (!lastLocation || !lastLocation.status) {
				api.toast({
					msg : '暂未定位\n无法上传照片！'
				});
				window.setTimeout(function() {
					api.closeWin();
				}, 2000);
			}
			var lon1 = displaynum(lastLocation.lon);
			var lat1 = displaynum(lastLocation.lat);
			var curentTime2 = CurentTime2();
			$api.setStorage('newtime', curentTime2);
			$api.setStorage('lon1', lon1);
			$api.setStorage('lat1', lat1);
			var lon1 = $api.getStorage('lon1');
			var lat1 = $api.getStorage('lat1');

			imageUtil.add('camera', function(ret, err) {
				////////////////////////////////////////////
				var newtime = $api.getStorage('newtime');
				///////////////////////////////

				// wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx修改打水印照片预览地址
				var oldret = ret.data.split(".");
				var newret = oldret[0]+"_shuiyin;"+user.userid+";"+projectid+";"+projectname+";"+lon1+";"+lat1+";"+lastLocation.photoaddress+";"+newtime+";."+oldret[1];
				console.log(newret);

				// wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
				var imageInfo = {
					userid : user.userid,
					detailid : projectid,
					itemname : projectname,
					imgpath : newret,
					test : '',
					lon : lon1,
					lat : lat1,
					photoaddress : lastLocation.photoaddress,
					phototime : (new Date()).pattern("yyyy-MM-dd HH:mm:ss")
				};
				imageUtil.getImageArray().push(imageInfo);
				showlog('添加一张待上传图片,待上传图片数组长度:' + imageUtil.getImageArray().length);
				setTimeout(function(){
					renderTemp('list-view', 'img-template', imageInfo, true);
					$('#add').parent().insertAfter('#list-view li:last');
				},1000);
			});
		}

		/*
		 * 下载图片
		 */
		function downloadPic() {

			imageUtil.download(function(imageData, ret, err) {
				if (ret) {
					if (ret.status) {
						console.log("图片路径：" + ret.url);
						var imageInfo = {
							imgpath : ret.url,
							lon : imageData.lon,
							lat : imageData.lat,
							phototime : imageData.phototime,
							photoaddress : imageData.photoaddress,
							isDel : false
						};
						imageUtil.getImageArray().push(imageInfo);
						renderTemp('list-view', 'img-template', imageInfo, true);
					} else {
						api.toast({
							msg : '加载图片失败！'
						});
					}
				} else {
					api.toast({
						msg : '缓存图片失败！'
					});
				}
			});
		}

		/**
		 *删除图片
		 */
		function removeImg(imgPath) {
			var imageArray = imageUtil.getImageArray();
			var size = imageArray.length;
			for (var i = 0; i < size; i++) {
				var data = imageArray[i];
				if (data.imgpath == imgPath) {
					imageArray.remove(i);
					break;
				}
			}
			$(event.target).parents('li').remove();
		}

		function call_upload(result) {
			if (result == '提交文本和图片') {
				submission = 'BOTH_SUBMIT_TEXT_IMAGE';
				formUtil.submit();
			} else if (result == '仅提交文本') {
				submission = 'ONLY_SUBMIT_TEXT';
				formUtil.submit();
			}
		}

		function call_validate() {
			if (formUtil.validate()) {
				select();
			}
		}

		/**
		 *  选择提交方式：1.提交文本和图片，2. 仅提交文本
		 */
		function select() {
			$aui.dialog({
				title : '提交确认',
				text : '确认提交现场检查数据？'
			}, function(ret) {
				if (ret.buttonIndex) {
					var optList = ['提交文本和图片', '仅提交文本'];
					var cbTarget = {
						name : api.winName,
						frameName : 'checkProjectDetail_frm',
						cbFn : 'call_upload'
					};
					$api.setStorage('optList', optList);
					$api.setStorage('cbTarget', cbTarget);
					api.openFrame({
						name : 'popup_select_frm',
						url : '../dialog/popup_select.html',
						pagePara : api.pageParam,
						bounces : false
					});
				}
				$aui.hideDialog();
			});
		}

				/*
		 * 巡检任务详情回滚
		 */
		function call_rollback() {
			var opts = {
				detailid : api.pageParam.detailid
			};
			api.confirm({
				title : '重置确认',
				msg : '确认重置该现场检查项目吗？'
			}, function(ret) {
				if (ret.buttonIndex == 2) {
					showMask("重置...");
					$client.checkProjectRollback(opts, function(ret, err) {
//					$api.setStorage("rollback","Y");
					console.log("chongzhi:"+$api.getStorage("rollback"));
						hideMask();
						if (ret) {
							if (ret.success) {
								window.setTimeout(function() {
									var pageParam = {};
									$.extend(pageParam, api.pageParam);
									pageParam.iscontent = '11';
									pageParam.isrollback="Y";
									api.openWin({
										name : 'check_projectDetail',
										url : 'checkProjectDetail_win.html',
										pageParam : pageParam,
										reload : true
									});
//									api.execScript({
//										name : 'check_projectList',
//										frameName : 'checkProjectListFrm',
//										script : 'refreshList();'
//									});
									api.execScript({
										name : 'check_projectDetail',
										script : 'changKey();'
									});
								}, 200);
							} else {
								api.toast({
									msg : ret.data_info
								});
							}
						} else {
							api.toast({
								msg : err.msg
							});
						}

					});
				}
			});
		}
	</script>
</html>
